package com.fram.service.api.v1.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fram.bean.admin.ordermain.response.OrderResponse;
import com.fram.bean.api.recovery.request.RecoveryFormRequest;
import com.fram.bean.api.recovery.response.RecoveryApiListResponse;
import com.fram.bean.api.recovery.response.RecoveryResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.base.BaseController;
import com.fram.entity.RecoveryForm;
import com.fram.mapper.OrderMainMapper;
import com.fram.mapper.RecoveryFormMapper;
import com.fram.service.api.v1.RecoveryForService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class RecoveryForServiceImpl extends BaseController implements RecoveryForService  {
	
	@Autowired
	private RecoveryFormMapper recoveryFormMapper;
	@Autowired
	private OrderMainMapper OrderMainMapper;
	/**添加  收回的 商品**/
//	@Override
//	public void add(List<RecoveryFormRequest> request) throws ProException {
//		RecoveryForm re=new RecoveryForm();
//		for (RecoveryFormRequest recovery : request) {
//			re.setUserId(getApiUserId());
//			re.setDistributorId(recovery.getDistributorId());
//			re.setOrderId(recovery.getOrderId());
//			for (GoodsResponse  goods:recovery.getGoods()) {
//				re.setGoodsId(goods.getGoodsId());
//				re.setGoodsNumber(goods.getGoodsNumber());
//				if(recoveryFormMapper.insertSelective(re)<=0) {
//					throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
//				}
//			}
//		}
//	}
	
	@Override
	public PageInfo<RecoveryResponse> list(PageBean<RecoveryForm> request) throws ProException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<RecoveryResponse> list = recoveryFormMapper.listForApi(request.getParam());
		if (list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}
	
	/** 可回收的 列表**/
	@Override
	public List<OrderResponse> Recyclable() throws ProException {
	 	List<OrderResponse> list=OrderMainMapper.selectByRecyclable(getApiUserId());
	 	if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
	 	}
		return list;
	}

	@Override
	public void adds(List<RecoveryFormRequest> request) throws ProException {
		for (RecoveryFormRequest recoveryFormRequest : request) {
			RecoveryForm recov = new RecoveryForm();
			recov.setOrderId(recoveryFormRequest.getOrderId());
			recov.setGoodsId(recoveryFormRequest.getGoodsId());
			recov.setDistributorId(recoveryFormRequest.getDistributorId());
			recov.setUserId(getApiUserId());
			if (recoveryFormMapper.insertSelective(recov) <= 0) {
				throw new ProValiDataException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
			}
		}
	}

	@Override
	public List<RecoveryApiListResponse> Recyclables() throws ProException {
		List<RecoveryApiListResponse> list=OrderMainMapper.selectByRecyclables(getApiUserId());
	 	if(list == null || list.size()==0) {
			throw new ProValiDataException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
	 	}
		return list;
	}
}
